﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using StockMonitor.Helper;
using System.Windows.Forms;
using Tigera.Framework.Utility;
using Tigera.T_Definition;
using Tigera.LibCommon;
namespace StockMonitor
{



    /// <summary>
    /// 用户身份信息管理
    /// </summary>
    /// <remarks>
    /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
    /// 编写时间：<datetime>2012-10-16 5:04 PM</datetime>
    /// </remarks>
    public class AuthUserService
    {
        #region Members

        public static AuthUserService Instance = new AuthUserService();

        private AuthUserService()
        {
            
        }

        
        
        #endregion

        #region Properties

        /// <summary>
        /// 资产帐号
        /// </summary>
        public string FundAccount { get; set; }
        
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 客户姓名
        /// </summary>
        public string ClientName { get; set; }

        /// <summary>
        /// 资金类型
        /// </summary>
        public string MoneyType { get; set; }

        /// <summary>
        /// 当前余额
        /// </summary>
        public double CurrentBalance { get; set; }

        /// <summary>
        /// 可用资金
        /// </summary>
        public double EnableBalance { get; set; }

        /// <summary>
        /// 可取现金
        /// </summary>
        public double FetchCash { get; set; }

        /// <summary>
        /// 资产总值
        /// </summary>
        public double AssetBalance { get; set; }

        /// <summary>
        /// 上海股东帐号
        /// </summary>
        public string SHStockAccount { get; set; }

        public double NetUnitValue
        {
            get
            {
                var total = double.Parse( AppMgr.GetSetting("configuration", "totalnum") ); 
                
                return AssetBalance / total;

            }

        }

        /// <summary>
        /// 深圳股东帐号
        /// </summary>
        public string SZStockAccount { get; set; }

        #endregion

        /// <summary>
        /// 登陆，使用【F200客户登录校验】登陆资金帐户。
        /// 并使用【F405查询资金】查询资金。
        /// 使用【F407查询股东】查询沪深股票帐号。
        /// </summary>
        /// <param name="account">用户名.</param>
        /// <param name="pwd">密码.</param>
        /// <returns></returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 5:04 PM</datetime>
        /// </remarks>
        /// 
        bool _logined;
        public bool Logined
        {
            get { return _logined; }
            set { _logined = value; }
        }
        
        
        NetBasicAccountInfo m_info = new NetBasicAccountInfo();


        public void QueryUserInfo()
        {
            
            
                TradeCMD cmd = new TradeCMD();
                cmd.Ope = TradeRiskOpe.TradeQueryAccountInfoQuery;
                AppMgr.Instance.Mqclient.OnBasicAccountInfo -= Mqclient_OnBasicAccountInfo;
                AppMgr.Instance.Mqclient.OnBasicAccountInfo += new EventHandler<TiEventArgs<NetBasicAccountInfo>>(Mqclient_OnBasicAccountInfo);
                AppMgr.Instance.Mqclient.SendDataTo(AppMgr.TradeRiskExName, ESHelper.Pack(ref cmd));           
            
            
        }

        public void NetGetPosInfo()
        {
           
                TradeCMD cmd = new TradeCMD();
                cmd.Ope = TradeRiskOpe.AccountPositionQuery;
                AppMgr.Instance.Mqclient.OnList_403 -= Mqclient_OnList_403;
                AppMgr.Instance.Mqclient.OnList_403 += new EventHandler<TiEventArgs<List<ResultF403>>>(Mqclient_OnList_403);
                AppMgr.Instance.Mqclient.SendDataTo(AppMgr.TradeRiskExName, ESHelper.Pack(ref cmd));
                

            
        }

        void Mqclient_OnList_403(object sender, TiEventArgs<List<ResultF403>> e)
        {
           
        }
        /*
        public bool Login(string account, string pwd)
        {
            {


                try
                {
                    var result = HSCOM.hS.F200客户登录校验(HsGateway.op_branch_no, HsGateway.branch_no, "1", pwd, "0", account);
                    var result2 = HSCOM.hS.F405查询资金(HsGateway.op_branch_no, HsGateway.branch_no, result.fund_account, pwd, "0");
                    var result3 = HSCOM.hS.F407查询股东(HsGateway.op_branch_no, HsGateway.branch_no, result.fund_account, pwd, "", "", "", 5, "");



                    Instance.FundAccount = account;
                    Instance.Password = pwd;
                    Instance.ClientName = result.client_name;
                    Instance.MoneyType = result.money_type == "0" ? "人民币" : result.money_type == "1" ? "美元" : "港币";


                    Instance.CurrentBalance = result2.First().current_balance;
                    Instance.EnableBalance = result2.First().enable_balance;
                    Instance.FetchCash = result2.First().fetch_cash;
                    Instance.AssetBalance = result2.First().asset_balance;


                    Instance.SHStockAccount = result3.Find(f => f.exchange_type == "1") != null ? result3.Find(f => f.exchange_type == "1").stock_account : string.Empty;
                    Instance.SZStockAccount = result3.Find(f => f.exchange_type == "2") != null ? result3.Find(f => f.exchange_type == "2").stock_account : string.Empty;

                    return true;
                }
                catch (HsGateway.HsCommException hsex)
                {
                    MessageBox.Show("错误：" + hsex.ToString());
                    return false;
                }

                catch (System.Net.WebException wex)
                {
                    MessageBox.Show("网络错误：" + wex.Message);
                    return false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("未知错误：" + ex.Message);
                    return false;
                }
            }
        }
         */
        public event EventHandler OnBasicACCountInfoRec;

        void Mqclient_OnBasicAccountInfo(object sender, TiEventArgs<NetBasicAccountInfo> e)
        {

            Instance.FundAccount = e.Data.FundAccount;
            Instance.Password = e.Data.Pwd;
            Instance.ClientName = e.Data.ClientName;
            Instance.MoneyType = "人民币";//result.money_type == "0" ? "人民币" : result.money_type == "1" ? "美元" : "港币";


            Instance.CurrentBalance = e.Data.CurrentBalance;
            Instance.EnableBalance = e.Data.EnableBalance;
            Instance.FetchCash = e.Data.FetchCash;
            Instance.AssetBalance = e.Data.AssetBalance;
            

            if (OnBasicACCountInfoRec != null)
            {
                OnBasicACCountInfoRec(this, EventArgs.Empty);
            }
                 

            
        }

        /// <summary>
        /// 查询所持沪、深股票仓位.
        /// </summary>
        /// <returns>持仓列表</returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 5:07 PM</datetime>
        /// </remarks>
        /// 
        /*
        public List<ResultF403> QueryStockPosistion()
        {
            
            try
            {
                List<ResultF403> result = HSCOM.hS.F403查询股票(
                    HsGateway.op_branch_no
                    , HsGateway.branch_no
                    , int.Parse(FundAccount)
                    , Password
                    , string.Empty
                    , SHStockAccount
                    , string.Empty
                    , string.Empty
                    , "0"
                    , 1000
                    , string.Empty);

                //result.AddRange(HSCOM.hS.F403查询股票(
                //    HsGateway.op_branch_no
                //    , HsGateway.branch_no
                //    , int.Parse(FundAccount)
                //    , Password
                //    , string.Empty
                //    , SZStockAccount
                //    , string.Empty
                //    , string.Empty
                //    , "0"
                //    , 1000
                //    , string.Empty));

                return result;
            }
            catch (HsGateway.HsCommException hsex)
            {
                //MessageBox.Show("错误：" + hsex.ErrorInfo.ToString());

                //Log.Exception("错误", hsex);
                return null;
            }
            catch (System.Net.WebException wex)
            {
                //MessageBox.Show("网络错误：" + wex.Message);

                //Log.Exception("网络错误", wex);
                return null;
            }
            catch (Exception ex)
            {
                //MessageBox.Show("未知错误：" + ex.Message);

                //Log.Exception("未知错误", ex);
                return null;
            }
          
        }
           * */
        /// <summary>
        /// 刷新资金帐户余额.
        /// </summary>
        /// <returns>是否成功</returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 5:08 PM</datetime>
        /// </remarks>
        /// 
        /*
        public bool RefreshBalance()
        {
            
            try
            {
                var result = HSCOM.hS.F405查询资金(HsGateway.op_branch_no, HsGateway.branch_no, int.Parse(FundAccount), Password, "0");
                if (result == null)
                    return false;

                //update data.
                Instance.CurrentBalance = result.First().current_balance;
                Instance.EnableBalance = result.First().enable_balance;
                Instance.FetchCash = result.First().fetch_cash;
                Instance.AssetBalance = result.First().asset_balance;

                return true;
            }
            catch
            {
                throw;
            }
         
        }
             *     */
    }
}
